package com.alphacsp.theedge2012.network;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import com.alphacsp.theedge2012.data.entities.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.DateUtils;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Gilad
 * Date: 20/10/10
 * Time: 15:21
 */
public class WebserviceClient {

    public static final String SPEAKERS_SERVICE_URL = "http://www.javaedge.com/service/getSpeakers";
    public static final String SESSIONS_SERVICE_URL = "http://www.javaedge.com/service/getSessions";
    public static final String SCHEDULE_SERVICE_URL = "http://www.javaedge.com/service/getSchedule";

    public static List<Speaker> fetchSpeakers() {
        InputStream inputStream = executeGetRequest(SPEAKERS_SERVICE_URL);
        Speakers speakers = XMLParser.parseXML(inputStream);
        if (speakers == null) {
            return Collections.emptyList();
        }
        return speakers.getSpeakerList();
    }

    public static List<Session> fetchSessions() {
        InputStream inputStream = executeGetRequest(SESSIONS_SERVICE_URL);
        Sessions sessions = XMLParser.parseXML(inputStream);
        if (sessions == null) {
            return Collections.emptyList();
        }
        return sessions.getSessionsList();
    }

    public static List<Event> fetchSchedule() {
        InputStream inputStream = executeGetRequest(SCHEDULE_SERVICE_URL);
        Schedule schedule = XMLParser.parseXML(inputStream);
        if (schedule == null) {
            return Collections.emptyList();
        }
        final List<Event> eventList = schedule.getEventList();
        for (Event event : eventList) {
            final int track = event.getTrack();
            if (track == 1) {
                event.setTrackColor(Color.parseColor("#18b6e6"));
            } else if (track == 2) {
                event.setTrackColor(Color.parseColor("#df1831"));
            } else if (track == 3) {
                event.setTrackColor(Color.parseColor("#00a549"));
            } else {
                event.setTrackColor(Color.parseColor("#DBDB00"));
            }
        }
        return eventList;
    }

    public static Bitmap fetchImage(String url) {
        try {
            final byte[] bytes = EntityUtils.toByteArray(executeGetRequestEntity(url));
            return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        } catch (IOException e) {
            Log.e(WebserviceClient.class.getSimpleName(), "Error fetching image", e);
        }
        return null;
    }

    private static HttpEntity executeGetRequestEntity(String url) {
        DefaultHttpClient client = new DefaultHttpClient();
        try {
            URI uri = new URI(url);
            final HttpGet request = new HttpGet(uri);
            final HttpResponse response = client.execute(request);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                return response.getEntity();
            }
        } catch (Exception e) {
            Log.e(WebserviceClient.class.getSimpleName(), "Error executing request", e);
        }
        return null;
    }

    public static Date getLastModifiedDate(String url) {
        DefaultHttpClient client = new DefaultHttpClient();
        try {
            URI uri = new URI(url);
            final HttpGet request = new HttpGet(uri);
            final HttpResponse response = client.execute(request);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                return DateUtils.parseDate(response.getHeaders("Last-Modified")[0].getValue());
            }
        } catch (Exception e) {
            Log.e(WebserviceClient.class.getSimpleName(), "Error executing request", e);
        }
        return null;
    }

    private static InputStream executeGetRequest(String url) {
        try {
            return executeGetRequestEntity(url).getContent();
        } catch (IOException e) {
            Log.e(WebserviceClient.class.getSimpleName(), "Error executing request", e);
        }
        return null;
    }
}
